<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <table border="1">
            <tr>
                <th>序号</th>
                <th>商品名称</th>
                <th>商品数量</th>
                <th>商品价格</th>
                <th>商品上架时间</th>
                <th>操作</th>
            </tr>
            <tr v-for="(item,index) in indexdata">
                <td>{{index + 1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.number}}</td>
                <td>{{item.price}}</td>
                <td>{{item.addtime}}</td>
                <td>
                    <button @click="edit(item.id)">编辑</button>
                    <button @click="removegoods(item.id)">删除</button>
                </td>
            </tr>
        </table>
        <button @click="addgoods">添加商品</button>
        <div v-show="isshow">
            <span>商品名称</span>
            <input type="text" v-model="name"><br>
            <span>商品数量</span>
            <input type="text" v-model="number"><br>
            <span>商品价格</span>
            <input type="text" v-model="price"><br>
            <span>商品上架时间</span>
            <input type="text" v-model="addtime"><br>
            <button @click="addcontent">添加</button>
        </div>
        <div class="edit">
            <span>商品名称</span>
            <input type="text" v-model="name"><br>
            <span>商品数量</span>
            <input type="text" v-model="number"><br>
            <span>商品价格</span>
            <input type="text" v-model="price"><br>
            <span>商品上架时间</span>
            <input type="text" v-model="addtime"><br>
            <button @click="editcontent">更新</button>
        </div>
    </div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.4/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            indexdata: [],
            isshow: false,
            name: "",
            number: "",
            price: "",
            addtime: "",
            id:""
        },
        methods: {
            fetchListData() {
                $.get('http://127.0.0.1:4000/index', (data) => {
                    this.indexdata = data;
                })
            },
            addgoods() {
                this.isshow = true;
            },
            //编辑数据
            edit(id){
                $.post('http://127.0.0.1:4000/edit',{id},(res)=>{
                    let {id,name,number,price,addtime} =res[0];
                    this.id = id;
                    this.name= name;
                    this.number = number;
                    this.price = price;
                    this.addtime = addtime;
                })
            },
            editcontent(){
                let oldobj = {
                    id :this.id,
                    name: this.name,
                    number:this.number,
                    price: this.price,
                    addtime: this.addtime
                }
                $.post('http://127.0.0.1:4000/update', { data: oldobj }, (res) => {
                    let { code, message } = res
                    if (code === 200) {
                        alert(message)
                        this.fetchListData()
                    } else {
                        alert(message)
                    }
                })
            },
            // 删除数据
            removegoods(id) {
                $.post('http://127.0.0.1:4000/delete', {id}, (res) => {
                    let { code, message } = res;
                    if (code === 200) {
                        alert(message)
                        this.fetchListData()
                    } else {
                        alert(message)
                    }
                })
            },
            addcontent() {
                //    console.log(this.name,this.number,this.price,this.addtime);
                let newobj = {
                    name: this.name,
                    number: this.number,
                    price: this.price,
                    addtime: this.addtime
                }
                // 增加数据
                $.post('http://127.0.0.1:4000/addData', { data: newobj }, (res) => {
                    let { code, message } = res
                    if (code === 200) {
                        alert(message)
                        this.fetchListData()
                    } else {
                        alert(message)
                    }
                })
            }
        },
        created() {
            // 创建之后执行
            this.fetchListData()
        }

    })
</script>

</html>